package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.domain.Department;
import cn.wolfcode.crud.qo.PageResult;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.service.IDepartmentService;
import cn.wolfcode.crud.util.JsonResult;
import cn.wolfcode.crud.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    //表示有其中一个就可以访问该页面
    @RequiresPermissions(value = {"department:list","部门页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return  "department/list";
    }


    @RequestMapping("/delete")
    @RequiresPermissions(value = {"department:delete","部门删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){

            if (id != null) {
                departmentService.delete(id);
            }
            return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"department:saveOrUpdate","部门新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Department department){

            if (department.getId() != null) {
                departmentService.update(department);
            }else {
                departmentService.save(department);
            }
            return new JsonResult();

    }
}
